#!/bin/python
# file_path = './aiding.woff'
# with open(file_path,'rb') as f:
#     data = f.read()
#     print(type(data))
#     print(data.decode('ISO-8859-1'))
import requests,json,base64
from fontTools.ttLib import TTFont


# 第二页
# default_dict_2 = {'&#xc378':1,'&#xa987':2,'&#xb389':3,'&#xc374':4,'&#xb986':5,'&#xc253':6,'&#xe185':7,'&#xb219':8,'&#xa632':9,'&#xe936':0}
# # 第三页
# default_dict_3 = {'&#xa178':1,'&#xf375':2,'&#xc853':3,'&#xe432':4,'&#xe456':5,'&#xb827':6,'&#xa834':7,'&#xa649':8,'&#xf853':9,'&#xa396':0}

# print(default_dict)
# for i in default_dict:
#     print(i,default_dict[i])
total_number = 0
for i in range(1,101):
    response = requests.post('https://www.python-spider.com/api/challenge13',data={'page':i})
    resdata = json.loads(response.content)
    if 'woff' in resdata:
        ttf_path = r'D:\TestCode\self_spider\temp.ttf'
        with open(ttf_path, 'wb') as f:
            f.write(base64.b64decode(resdata['woff']))
        font = TTFont(ttf_path)
        default_dict = dict()
        default_list = [1,2,3,4,5,6,7,8,9,0]
        j=0
        for value in font.get('post').__dict__.get('extraNames'):
            default_dict[value.replace('uni','&#x')] = default_list[j]
            j+=1
            # print(value.replace('uni','&#x'))
        # print(default_dict)

    if 'data' in resdata:
        for datai in resdata['data']:

            if datai['value'] is not None:
                temp = ''
                temp_list = []
                for childdata in datai['value'].strip().split(" "):
                    # print('childdata',childdata)
                    temp_list.append(str(default_dict[childdata]))
                # print(temp_list,temp.join(temp_list))
                total_number = total_number+ int(temp.join(temp_list))
            else:
                pass
    else:
        pass

    print('第  %d  页 ' % i)

print(total_number)




